package com.boco.modules.fdoc.web.outpatient;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.boco.common.json.BaseJsonVo;
import com.boco.common.persistence.Page;
import com.boco.common.utils.JsonUtils;
import com.boco.modules.fdoc.model.HospitalEntity;
import com.boco.modules.fdoc.service.HospitalService;
import com.boco.modules.fdoc.service.OutpatientOrderService;
import com.boco.modules.fdoc.vo.OutpatientOrderVo;
import com.boco.modules.fdoc.vo.ServicepackVo;

/**
 * Description: 预约挂号管理
 * @author lzz
 *
 */
@Controller
@RequestMapping(value="/outpatientOrder",produces = "application/json;charset=UTF-8")
public class OutpatientOrderController {
	
	@Resource
	HospitalService hospitalService;
	@Resource
	OutpatientOrderService opOrderService;
	
	/**
	 * 跳转主页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/showPage",method = RequestMethod.GET)
	public String showPage(HttpServletRequest request, Model model){
		//获取医院列表
		List<HospitalEntity> hospitalList = hospitalService.getHospitalList();
		//设置作用域对象
		model.addAttribute("hospitalList", hospitalList);
		return "/outpatient/outpatientOrder";
	}
	
	/**
	 * 获取预约列表
	 * @param request
	 * @param model
	 * @param orgId 医院id
	 * @param startTime 查询开始时间
	 * @param endTime 查询结束时间
	 * @param pageIndex 页码
	 * @param pageSize 页面大小
	 * @return
	 * @throws ParseException 日期转换异常
	 */
	@ResponseBody
	@RequestMapping(value = "/getOrderList",method = RequestMethod.GET)
	public String getOrderList(HttpServletRequest request, Model model,String orgId,String  startTime,String  endTime,
				Integer pageIndex,Integer pageSize) throws ParseException{
		
		OutpatientOrderVo countVo=new OutpatientOrderVo();
		OutpatientOrderVo newVo=new OutpatientOrderVo();
		Page<OutpatientOrderVo> page = new Page<>(pageIndex, pageSize);
		newVo.setPage(page);
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			
		Date sdate=null;
		Date edate=null;
		if(startTime!=null && !"".equals(startTime)){
			Date date1=sdf.parse(startTime);
			Calendar stTime=Calendar.getInstance();
			stTime.set(Calendar.YEAR, date1.getYear()+1900);
			stTime.set(Calendar.MONTH, date1.getMonth());
			stTime.set(Calendar.DAY_OF_MONTH,date1.getDate());
			stTime.set(Calendar.HOUR_OF_DAY, 0);  
			stTime.set(Calendar.MINUTE, 0);  
			stTime.set(Calendar.SECOND, 0);
			stTime.set(Calendar.MILLISECOND, 0);
			sdate=new Date(stTime.getTime().getTime());
		}
		if(endTime!=null && !"".equals(endTime)){
			Date date2=sdf.parse(endTime);
			Calendar edTime=Calendar.getInstance();
			edTime.set(Calendar.YEAR, date2.getYear()+1900);
			edTime.set(Calendar.MONTH, date2.getMonth());
			edTime.set(Calendar.DAY_OF_MONTH,date2.getDate());
			edTime.set(Calendar.HOUR_OF_DAY, 23);  
			edTime.set(Calendar.MINUTE, 59);  
			edTime.set(Calendar.SECOND, 59);
			edTime.set(Calendar.MILLISECOND, 999);
			edate=new Date(edTime.getTime().getTime());
		}
			
		newVo.setOrgId(orgId);
		newVo.setEndTime(edate);
		newVo.setStartTime(sdate);
		countVo.setOrgId(orgId);
		countVo.setEndTime(edate);
		countVo.setStartTime(sdate);
		List<OutpatientOrderVo> list=opOrderService.getOrderList(newVo);
		Integer count =opOrderService.getCount(countVo);
		return JsonUtils.getJson(BaseJsonVo.pageList(list, count));
		
	}

}
